package com.edu.exam.entity.quality.monitoring;

import com.edu.exam.enums.common.OperationType;
import com.edu.exam.enums.grading.audit.OperationLevel;
import com.edu.exam.enums.common.OperationResult;
import com.edu.exam.enums.quality.monitoring.TargetType;
import com.edu.exam.enums.system.RiskLevel;
import com.edu.exam.enums.quality.monitoring.SeverityLevel;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

/**
 * 分配审计实体类
 *
 * 根据E-06-01任务分配子模块设计实现
 * 对应allocation_audit数据表，用于记录所有分配操作的审计日志
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class AllocationAudit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "audit_id", length = 50, unique = true, nullable = false)
    private String auditId;

    @Enumerated(EnumType.STRING)
    @Column(name = "operation_type", length = 50, nullable = false)
    private OperationType operationType;

    @Column(name = "operator_id", length = 50, nullable = false)
    private String operatorId;

    @Column(name = "operator_name", length = 100)
    private String operatorName;

    @Column(name = "operator_role", length = 50)
    private String operatorRole;

    @Enumerated(EnumType.STRING)
    @Column(name = "target_type", length = 20, nullable = false)
    private TargetType targetType;

    @Column(name = "target_id", length = 50, nullable = false)
    private String targetId;

    @Column(name = "target_name", length = 200)
    private String targetName;

    @Column(name = "operation_data", columnDefinition = "TEXT")
    private String operationData; // JSON格式

    @Column(name = "old_value", columnDefinition = "TEXT")
    private String oldValue;

    @Column(name = "new_value", columnDefinition = "TEXT")
    private String newValue;

    @Enumerated(EnumType.STRING)
    @Column(name = "operation_result", length = 20, nullable = false)
    private OperationResult operationResult;

    @Column(name = "result_message", columnDefinition = "TEXT")
    private String resultMessage;

    @Column(name = "error_code", length = 50)
    private String errorCode;

    @Column(name = "error_details", columnDefinition = "TEXT")
    private String errorDetails;

    @Column(name = "ip_address", length = 50)
    private String ipAddress;

    @Column(name = "user_agent", length = 500)
    private String userAgent;

    @Column(name = "session_id", length = 100)
    private String sessionId;

    @Column(name = "request_id", length = 100)
    private String requestId;

    @Column(name = "transaction_id", length = 100)
    private String transactionId;

    @Column(name = "operation_time", nullable = false)
    private LocalDateTime operationTime;

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    // 扩展字段
    @Column(name = "batch_operation_id", length = 100)
    private String batchOperationId;

    @Column(name = "parent_audit_id")
    private Long parentAuditId;

    @Column(name = "correlation_id", length = 100)
    private String correlationId;

    @Enumerated(EnumType.STRING)
    @Column(name = "risk_level", length = 20)
    private RiskLevel riskLevel;

    @Enumerated(EnumType.STRING)
    @Column(name = "severity_level", length = 20)
    private SeverityLevel severityLevel;

    @Column(name = "impact_scope", length = 20)
    private String impactScope;

    @Column(name = "affected_users_count")
    private Integer affectedUsersCount;

    @Column(name = "affected_records_count")
    private Integer affectedRecordsCount;

    @Column(name = "execution_duration")
    private Long executionDuration; // 执行时长（毫秒）

    @Column(name = "success")
    private Boolean success;

    @Column(name = "requires_review")
    private Boolean requiresReview;

    @Column(name = "reviewed")
    private Boolean reviewed;

    @Column(name = "reviewed_by", length = 50)
    private String reviewedBy;

    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    @Column(name = "review_comments", columnDefinition = "TEXT")
    private String reviewComments;

    @Column(name = "additional_context", columnDefinition = "TEXT")
    private String additionalContext;

    @Column(name = "business_rules", columnDefinition = "TEXT")
    private String businessRules; // 应用业务规则（JSON格式）

    @Column(name = "system_info", columnDefinition = "TEXT")
    private String systemInfo; // 系统信息（JSON格式）

    @Column(name = "compliance_flags", columnDefinition = "TEXT")
    private String complianceFlags; // 合规性标记（JSON格式）

    @Column(name = "retention_days")
    private Integer retentionDays; // 保留天数

    @Column(name = "archived")
    private Boolean archived;

    @Column(name = "archive_time")
    private LocalDateTime archiveTime;

}

